package com.fw.payment.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import com.fw.payment.util.StringUtil;
import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.serviceprofile.bean.ServiceProfileBean;
import com.fw.util.StringUtils;

public class ReponseBackgroundController {

    public static final Integer REDIRECT_SUCCESS = 1;
    public static final Integer REDIRECT_CANCEL = 2;

    private static final Logger log = Logger.getLogger(ReponseBackgroundController.class);

    public String returnView(String view, InboundMessageLogBean inboundMessageLogBean, ServiceProfileBean serviceProfileBean) {
	return returnView(view, inboundMessageLogBean, serviceProfileBean, null);
    }

    public String returnView(String view, InboundMessageLogBean inboundMessageLogBean, ServiceProfileBean serviceProfileBean,
	    Integer redirectToMerchant) {
	log.info("begin returnView");
	if (serviceProfileBean != null && Boolean.TRUE.equals(serviceProfileBean.getIsResponseUrlBack())) {
	    String responseUrlBack = serviceProfileBean.getResponseUrlBack();
	    if (responseUrlBack != null) {

		log.info("INIT Response Background URL: " + responseUrlBack);
		try {

		    CloseableHttpClient client = HttpClientBuilder.create().build();
		    HttpPost post = new HttpPost(responseUrlBack);

		    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
		    urlParameters.add(new BasicNameValuePair("serviceID", inboundMessageLogBean.getServiceID()));
		    urlParameters.add(new BasicNameValuePair("merchantID", inboundMessageLogBean.getMerchantID()));
		    urlParameters.add(new BasicNameValuePair("respCode", "respCode"));
		    urlParameters.add(new BasicNameValuePair("fraudCode", "00"));
		    urlParameters.add(new BasicNameValuePair("Pan", "0000000000000000"));
		    urlParameters.add(new BasicNameValuePair("amount", inboundMessageLogBean.getAmount()));
		    urlParameters.add(new BasicNameValuePair("invoiceNo", StringUtil.getCashFormat(inboundMessageLogBean.getServiceID(),
			    inboundMessageLogBean.getInvoiceNo())));
		    urlParameters.add(new BasicNameValuePair("tranRef", "tranRef"));
		    urlParameters.add(new BasicNameValuePair("approvalCode", "approvalCode"));
		    urlParameters.add(new BasicNameValuePair("Eci", "Eci"));
		    urlParameters.add(new BasicNameValuePair("dateTime", StringUtils.formatDate(new Date(),
			    StringUtils.PATTERN_DATETIME_INTERFACE)));
		    urlParameters.add(new BasicNameValuePair("Status", "Status"));
		    urlParameters.add(new BasicNameValuePair("failReason", "failReason"));
		    urlParameters.add(new BasicNameValuePair("agentCode", "agentCode"));
		    urlParameters.add(new BasicNameValuePair("channelCode", "channelCode"));
		    urlParameters.add(new BasicNameValuePair("userDefined1", "userDefined1"));
		    urlParameters.add(new BasicNameValuePair("userDefined2", "userDefined2"));
		    urlParameters.add(new BasicNameValuePair("userDefined3", "userDefined3"));
		    urlParameters.add(new BasicNameValuePair("userDefined4", "userDefined4"));
		    urlParameters.add(new BasicNameValuePair("userDefined5", "userDefined5"));
		    urlParameters.add(new BasicNameValuePair("noteToMerchant", "noteToMerchant"));
		    urlParameters.add(new BasicNameValuePair("hashValue", "hashValue"));
		    urlParameters.add(new BasicNameValuePair("TypeOfCreditCard", "TypeOfCreditCard"));

		    post.setEntity(new UrlEncodedFormEntity(urlParameters));

		    HttpResponse response = client.execute(post);
		    log.info("Response Background URL: " + responseUrlBack + ", status code: " + response.getStatusLine().getStatusCode());
		    // BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		    //
		    // StringBuffer result = new StringBuffer();
		    // String line = "";
		    // while ((line = rd.readLine()) != null) {
		    // result.append(line);
		    // }
		    client.close();
		} catch (Exception e) {
		    log.error(e.getMessage(), e);
		    e.printStackTrace();
		}

	    }
	}
	log.info("end returnView");
	if (redirectToMerchant != null) {
	    String redirectURL = null;
	    if (REDIRECT_SUCCESS.equals(redirectToMerchant)) {
		if (serviceProfileBean != null && Boolean.TRUE.equals(serviceProfileBean.getIsSuccessUrl())
			&& serviceProfileBean.getSuccessUrl() != null) {
		    redirectURL = serviceProfileBean.getSuccessUrl();
		} else if (inboundMessageLogBean != null && inboundMessageLogBean.getSuccessURL() != null) {
		    redirectURL = inboundMessageLogBean.getSuccessURL();
		}
	    } else if (REDIRECT_CANCEL.equals(redirectToMerchant)) {
		if (serviceProfileBean != null && Boolean.TRUE.equals(serviceProfileBean.getIsCancelUrl())
			&& serviceProfileBean.getCancelUrl() != null) {
		    redirectURL = serviceProfileBean.getCancelUrl();
		} else if (inboundMessageLogBean != null && inboundMessageLogBean.getCancelURL() != null) {
		    redirectURL = inboundMessageLogBean.getCancelURL();
		}
	    }
	    if (redirectURL != null) {
		return "redirect:" + redirectURL;
	    } else {
		return view;
	    }
	} else {
	    return view;
	}
    }

}
